<script>
//关闭POP
$('close_state').addEvent('click', function(event){
    this.getParent('.dialog').retrieve('instance').close();
});

//表单项过滤
function itemFilter(str,patrn)
{
    if (patrn.exec(str)) return true;
    else return false;
}

//结算余额计算
$('paid').addEvent('blur',function(event){
    var payed = "<{$detail.paid}>";//获取已付款金额
    if(!payed){
        payed = 0;
     }
    var paid = parseFloat($('paid').get('value')).toFixed(3);//结算金额
    var pattrn = /^[0-9]{1,8}(.[0-9]{1,3})?$/;
    //过滤表单项
    if(itemFilter(paid,pattrn)==false && paid!=''){
        alert('结算金额只能输入数字，且最多为8位！');
        $('paid').set('value','');
        return false;
    }
    if (paid){
        var payable = parseFloat($('payable').get('value')).toFixed(3);//应付金额
        //var deposit = $('deposit').get('value');//预付金额
        var deposit = 0;
        var balance = parseFloat(payable - payed - paid).toFixed(3);
        if(balance < 0){
         alert('结算金额不能大于还需支付！');
         $('balance').set('value',0);
         $('balance_span').set('text',0);
         $('paid').set('value',0);
         return false;
        }
        //计算结算余额
        $('balance').set('value',balance);
        $('balance_span').set('text',balance);
    }else {
        $('balance').set('value','');
        $('balance_span').set('text','');
    }
});

//结算成功处理
function statementHand(){
    alert('结算成功');
    $('stateForm').getParent('.dialog').retrieve('instance').close();
    
}
//结算确认
function statementConfirm(){

    if(confirm('确定要执行此次结算吗？')){
        return true;
    }else return false;
}
//提交表单
function sendForm(event)
{
    var _form = $('stateForm');
    _form.set('action',_formActionURL).fireEvent('submit',new Event(event));
    statementHand();

    /*
    //提交结算申请
    _form.set('send', {
          url: _formActionURL, 
          method: 'post',
          onComplete: function(f){
            //alert(f);
            statementHand();
          },
          onError: function(err){
            alert('提交处理出错');
          }
    });
    _form.send();//发送表单数据
    */
}
//POP弹窗口
function popup(html)
{
    html = encodeURI(encodeURI(html));
    new Dialog('index.php?<{$env.server.QUERY_STRING|replace:"statement":"stateConfirm"}>&p[0]='+html,{
          title:'结算确认',
          width:300,
          height:180,
          modal:true,
          resizeable:false
    });
}
var _form = $('stateForm');
var _formActionURL = _form.get('action');

//结算判断
$('statement').addEvent('click', function(event){
    var payed = "<{$detail.paid}>";//获取已付款金额
    if(!payed){
        payed = 0;
     }

    //获取初始值:结算金额、结算余额、付款方式
    var paid = parseFloat($('paid').get('value')).toFixed(3);//结算金额
    var payable = parseFloat($('payable').get('value')).toFixed(3);//应付金额
    //var deposit = $('deposit').get('value');//预付金额
    var deposit = 0;
    var balance = parseFloat(payable - payed - deposit - paid).toFixed(3);//结算余额
    var payment = $('po_type').get('value');//付款方式

    //结算金额不能为0
    if (paid<=0){
        if (confirm('结算金额为0，确认要提交吗？') == false) {
            $('paid').set('value',payable);
            $('paid').focus();
            return false;
        }
    }

    //根据付款方式选择处理代码段
    if(paid && payment){

        //判断结算余额为0情况
          if (balance==0)//为0
          {

              //确认结算
              var sTrue = statementConfirm();
              if(sTrue==true){
                 //提交结算申请
                 sendForm(event);
              }
            
          }
          else//不为0
          {
              //判断结算余额是否为负数
              if(balance<0){
                 alert('结算余额为负数，请检查输入的结算金额是否正确！')
                 return false;
              }
              else
              {             
                 //弹出POP窗口，让操作员选择继续还是取消结算
                 var html = '您的结算金额：<span class="red">' + paid + '</span><br />当前结算余额：<span class="red">' + balance +'</span>';
                 html += '<br/>结算余额不为0，继续付款操作吗？';
                 popup(html);
              }
          }
    }
    else
    {
        alert('请输入结算金额！');
        $('paid').focus();
    }
    
});

</script>
<form id="stateForm" action="index.php?<{$env.server.QUERY_STRING}>" method="post">
<input type="hidden" name="payment_id" value="<{$detail.payment_id}>" />
<input type="hidden" name="is_deduction" id="is_deduction" value="false" />
<div class="tableform">
    <div class="division">
        <table width='100%' cellspacing="0" cellpadding="0">
            <tr>
                <th>采购单编号：</th>
                <td>
                <{$po_bn}>
                </td>
                <th>付款类型：</th>
                <td>
                <{ if $detail.po_type=='cash' or $detail.po_type=='' }>
                  <{input type="hidden" name="po_type" id="po_type" value="cash" }>
                现款
                <{else}>
                <{input type="hidden" name="po_type" id="po_type" value="{$detail.po_type}" }>
                预付款
                <{/if}>
                </td>
            </tr>
            <tr>
                <th>应付金额：</th>
                <td>
                <span class="amount" tips="商品总额(<span class='red'><{$detail.product_cost|cur}></span>)+物流费用(<span class='red'><{$detail.delivery_cost|cur}></span>)"><{$detail.payable|cur}></span>
                <{if $detail.po_type=='cash'}>
                =商品总额(<span class="red"><{$detail.product_cost|cur}></span>)+物流费用(<span class="red"><{$detail.delivery_cost|cur}></span>)
                <{/if}>
                
                <{input type="hidden" name="payable" id="payable" value="{$detail.payable}" }>
                </td>
                <th>
                <!-- 如果付款类型为：预付款，则显示 -->
                <{if $detail.po_type=='credit'}>
                    <!-- 预付金额： -->
                <{/if}>供应商：</th>
                <td>
                <!-- 如果付款类型为：预付款，则显示 -->
                <{if $detail.po_type=='credit'}>
                <{input type="hidden" name="deposit" id="deposit" value="{$detail.deposit}" }>
                   <!-- <{$detail.deposit}> -->
                <{/if}>
                
                <{$supplier['name']}>
                </td>
            </tr>
            <tr>
                <th>已付金额：</th>
                <td> <{$detail.paid|cur}></td>
                <th>还需支付：</th>
                <td><span><{$detail.need|replace:".000":""}></span></td>
            </tr>
            
            <tr>
             <td colspan="4"><div style="height:25px; padding-left:13px; line-height:25px; font-weight:bold; margin-bottom:6px; border-bottom:1px solid #ccc">结算操作</div></td>
            </tr>
            
            <tr>
                <th>结算金额：</th>
                <td>
                <input type="text" name="paid" id="paid" value="<{$detail.need|replace:".000":""}>" vtype="required" size="12" /> <em class="red">*</span>
                
                </td>
                <th>结算余额：</th>
                <td>
                <{input type="hidden" name="balance" id="balance" value=""}>
                <span id="balance_span">0</span>
                </td>
            </tr>
            <tr>
                <th>银行帐号：</th>
                <td><{input type="text" name="bank_no"  value="" }></td>
                <th>发票号码：</th>
                <td><{input type="text" name="tax_no"  value="" }></td>
            </tr>
            <tr>
                <th>物流单号：</th>
                <td><{input type="text" name="logi_no"  value="" }></td>
                <th></th>
                <td></td>
            </tr>
            <tr>
                <th>结算方式：</th>
                <td>
                <select name="payment">
                    <option value="">请选择</option>
                    <{foreach from=$payment item=item key=key}>
                    <option value="<{$item.id}>" <{if $item.id==$detail.payment}> selected <{/if}> ><{$item.custom_name}></option>
                    <{/foreach}>
                </select>
                </td>
                <th>经办人：</th>
                <td>
                <{input type="text" name="operator" id="operator" value="{$supplier['operator']}"}>
                </td>
            </tr>
            
            <tr>
                <th>备注：</th>
                <td colspan="3"><{input type="textarea" name="memo"  value="" row="8" col="80" }></td>
            </tr>
            
        </table>
   </div>
   <div class="table-action"><{button class="btn-primary" type="button" id="statement" label="付款" }>
                <{button class="btn-primary" type="button" id="close_state" label="取消" }>
                <{input type="hidden" name="statementSubmit" value="do" }>
</div>
</div>
</form>
